﻿// (c) Andreas Kreisl 
// You are not permitted to use this in any way!

var class_PercentBar_debug = false;

function class_PercentBar(myFrame,hOffset,vOffset,displayname) {
	this.style = preferences.mod_sysmonitor_bar_style.value;
	this.path = "Resources/PercentBar/" + this.style + "/";
	this.size    = mod_sysmonitor_size;
	this.vOffset = vOffset;
	this.hOffset = hOffset;

	this.peakdelay=0;
	this.value=0;
	this.peak=0;

	// Objects
	this.text_perc = new Text();
	this.text_perc.data = "0%";
	this.text_perc.size = this.size;
	this.text_perc.alignment = "right"
	this.text_perc.font = "Tahamo";
	this.text_perc.style.fontWeight = "bold";
	this.text_perc.color = "#FFFFFF";
	this.text_perc.hOffset = this.hOffset + ( 28 * this.size / 10 );
	this.text_perc.vOffset = this.vOffset + this.size;
	this.text_perc.width = ( 28 * this.size / 10 );
	myFrame.appendChild(this.text_perc);

	this.text_used = new Text();
	this.text_used.data = "";
	this.text_used.size = this.size;
	this.text_used.alignment = "right"
	this.text_used.font = "Tahamo";
	this.text_used.style.fontWeight = "bold";
	this.text_used.color = "#FFFFFF";
	this.text_used.hOffset = this.hOffset + mod_sysmonitor_width;
	this.text_used.vOffset = this.vOffset + this.size;
	this.text_used.width = ( 60 * this.size / 10 );
	this.text_used.truncation = "end";
	myFrame.appendChild(this.text_used);

	this.background = new Image();
	this.background.src = this.path + "bar.png";
	this.background.hOffset = this.hOffset + ( 28 * this.size / 10 );
	this.background.vOffset = this.vOffset;
	this.background.height = 12 * this.size / 10;
	this.background.width = 110 * this.size / 10;
	myFrame.appendChild(this.background);

	this.peak_left = new Image();
	this.peak_left.src = this.path + "bar__l.png";
	this.peak_left.hOffset = this.hOffset + ( 28 * this.size / 10 );
	this.peak_left.vOffset = this.vOffset;
	this.peak_left.height = 12 * this.size / 10;
	this.peak_left.width = 5 * this.size / 10;
	this.peak_left.colorize = "#505050";
	this.peak_left.orderAbove(this.background);
	this.peak_left.visible = false;
	myFrame.appendChild(this.peak_left);

	this.peak_middle = new Image();
	this.peak_middle.src = this.path + "bar__m.png";
	this.peak_middle.hOffset = this.peak_left.hOffset + this.peak_left.width;
	this.peak_middle.vOffset = this.vOffset;
	this.peak_middle.height = 12 * this.size / 10;
	this.peak_middle.width = this.peak * this.size / 10;
	this.peak_middle.colorize = "#505050";
	this.peak_middle.orderAbove(this.background);
	this.peak_middle.visible = false;
	myFrame.appendChild(this.peak_middle);

	this.peak_right = new Image();
	this.peak_right.src = this.path + "bar__r.png";
	this.peak_right.hOffset = this.peak_middle.hOffset + this.peak_middle.width;
	this.peak_right.vOffset = this.vOffset;
	this.peak_right.height = 12 * this.size / 10;
	this.peak_right.width = 5 * this.size / 10;
	this.peak_right.colorize = "#505050";
	this.peak_right.orderAbove(this.background);
	this.peak_right.visible = false;
	myFrame.appendChild(this.peak_right);

	this.left = new Image();
	this.left.src = this.path + "bar_l.png";
	this.left.hOffset = this.hOffset + ( 28 * this.size / 10 );
	this.left.vOffset = this.vOffset;
	this.left.height = 12 * this.size / 10;
	this.left.width = 5 * this.size / 10;
	this.left.orderAbove(this.peak_right);
	myFrame.appendChild(this.left);

	this.middle = new Image();
	this.middle.src = this.path + "bar_m.png";
	this.middle.hOffset = this.left.hOffset + this.left.width;
	this.middle.vOffset = this.vOffset;
	this.middle.height = 12 * this.size / 10;
	this.middle.width = this.value * this.size / 10;
	this.middle.orderAbove(this.left);
	myFrame.appendChild(this.middle);

	this.right = new Image();
	this.right.src = this.path + "bar_r.png";
	this.right.hOffset = this.middle.hOffset + this.middle.width;
	this.right.vOffset = this.vOffset;
	this.right.height = 12 * this.size / 10;
	this.right.width = 5 * this.size / 10;
	this.right.orderAbove(this.middle);
	myFrame.appendChild(this.right);

	this.text_name = new Text();
	this.text_name.data = displayname;
	this.text_name.size = this.size;
	this.text_name.style.textAlign = "center"
	this.text_name.font = "Tahamo";
	this.text_name.style.fontWeight = "bold";
	this.text_name.color = "#FFFFFF";
	//this.text_name.hOffset = this.hOffset + ( 83 * this.size / 10 );
	this.text_name.hOffset = this.left.hOffset + this.left.width;
	this.text_name.vOffset = this.vOffset + this.size;
	this.text_name.opacity = 128 + 64;
	this.text_name.width = 100 * this.size / 10;
	this.text_name.style.KONShadow="1px 1px rgba(0,0,0,128)";
		var shad = new Shadow();
		shad.color = "#000000";
		shad.opacity = 128;
		shad.hOffset = 1;
		shad.vOffset = 1;
		this.text_name.shadow = shad;
	this.text_name.orderAbove(this.right);
	myFrame.appendChild(this.text_name);


	// ---------------------------------------------------------------------------------------
	// Helper Functions


	this.PercentColor = function ( NewColor ) {
		if(this.text_perc.color != NewColor) {
			this.text_perc.color = NewColor;
			this.text_used.color = NewColor;
		}
	}


	this.PercentBarColor = function ( NewColor ) {
		if(this.left.colorize != NewColor) {
			this.left.colorize = NewColor;
			this.middle.colorize = NewColor;
			this.right.colorize = NewColor;
		}
	}


	this.PercentBarValue = function ( NewValue , NewText ) {
		if(isNaN(NewValue)) NewValue = 0;
		if(NewValue<0)      NewValue = 0;
		if(NewValue>100)    NewValue = 100;
		NewValue = Math.round(NewValue);

		this.value          = NewValue;
		this.middle.width   = this.value * this.size / 10;
		this.right.hOffset  = this.middle.hOffset + this.middle.width;
		this.text_perc.data = NewValue + "%";
		this.text_used.data = NewText;
	}


	this.PercentBarPeak = function ( NewValue ) {
		if(isNaN(NewValue)) NewValue = 0;
		if(NewValue<0)      NewValue = 0;
		if(NewValue>100)    NewValue = 100;

		if(this.peak != NewValue) {
			if(NewValue>0 & this.left.visible == true) {
				if(this.peak_left.visible==false) {
					this.peak_left.visible   = true;
					this.peak_middle.visible = true;
					this.peak_right.visible  = true;
				}

				this.peak               = NewValue;
				this.peak_middle.width  = this.peak * this.size / 10;
				this.peak_right.hOffset = this.peak_middle.hOffset + this.peak_middle.width;
			}else{
				this.peak_left.visible   = false;
				this.peak_middle.visible = false;
				this.peak_right.visible  = false;
			}
		}

	}


	this.PercentBarPeakFader = function ( step ) {
		if(this.value>this.peak) {
			step = this.value;
			if(arguments.length>1) {
				this.peakdelay=parseInt(arguments[1]);
			}
		} else {
			if(this.peakdelay>0) {
				this.peakdelay--;
				step = -1;
			} else {
				step = this.peak - step;
				if(this.peak < step) step = this.peak;
			}
		}
		if(step>-1) this.PercentBarPeak(step);
		return this.peak;
	}


	this.PercentBarVisible = function ( visible ) {
		this.text_perc.visible   = visible;
		this.text_used.visible   = visible;
		this.background.visible  = visible;
		this.peak_left.visible   = visible;
		this.peak_middle.visible = visible;
		this.peak_right.visible  = visible;
		this.left.visible        = visible;
		this.middle.visible      = visible;
		this.right.visible       = visible;
		this.text_name.visible   = visible;
		return true;
	}


	this.watch("style", function (p_name,p_old,p_new) {
		this.path = "Resources/PercentBar/" + p_new + "/";

		this.background.src  = this.path + "bar.png";
		this.peak_left.src   = this.path + "bar__l.png";
		this.peak_middle.src = this.path + "bar__m.png";
		this.peak_right.src  = this.path + "bar__r.png";
		this.left.src        = this.path + "bar_l.png";
		this.middle.src      = this.path + "bar_m.png";
		this.right.src       = this.path + "bar_r.png";

		return p_new;
	});


	this.watch("size", function (p_name,p_old,p_new) {
		this.text_perc.size = p_new;
		this.text_perc.hOffset = hOffset + ( 28 * p_new / 10 );
		this.text_perc.vOffset = this.vOffset + p_new;
		this.text_perc.width = ( 28 * p_new / 10 );

		this.text_used.size = p_new;
		this.text_used.hOffset = this.hOffset + mod_sysmonitor_width;
		this.text_used.vOffset = this.vOffset + p_new;
		this.text_used.width = ( 60 * p_new / 10 );

		this.background.hOffset = this.hOffset + ( 28 * p_new / 10 );
		this.background.height = 12 * p_new / 10;
		this.background.width = 110 * p_new / 10;

		this.peak_left.hOffset = this.hOffset + ( 28 * p_new / 10 );
		this.peak_left.height = 12 * p_new / 10;
		this.peak_left.width = 5 * p_new / 10;

		this.peak_middle.hOffset = this.peak_left.hOffset + this.peak_left.width;
		this.peak_middle.height = 12 * p_new / 10;
		this.peak_middle.width = this.peak * p_new / 10;

		this.peak_right.hOffset = this.peak_middle.hOffset + this.peak_middle.width;
		this.peak_right.height = 12 * p_new / 10;
		this.peak_right.width = 5 * p_new / 10;

		this.left.hOffset = this.hOffset + ( 28 * p_new / 10 );
		this.left.height = 12 * p_new / 10;
		this.left.width = 5 * p_new / 10;

		this.middle.hOffset = this.left.hOffset + this.left.width;
		this.middle.height = 12 * p_new / 10;
		this.middle.width = this.value * p_new / 10;

		this.right.hOffset = this.middle.hOffset + this.middle.width;
		this.right.height = 12 * p_new / 10;
		this.right.width = 5 * p_new / 10;

		this.text_name.size = p_new;
		//this.text_name.hOffset = this.hOffset + ( 83 * p_new / 10 );
		this.text_name.hOffset = this.left.hOffset + this.left.width;
		this.text_name.vOffset = this.vOffset + p_new;
		this.text_name.width = ( 100 * p_new / 10 );

		return p_new;
	});


	this.watch("vOffset", function (p_name,p_old,p_new) {
		this.text_perc.vOffset   = p_new + this.size;
		this.text_used.vOffset   = p_new + this.size;
		this.background.vOffset  = p_new;
		this.peak_left.vOffset   = p_new;
		this.peak_middle.vOffset = p_new;
		this.peak_right.vOffset  = p_new;
		this.left.vOffset        = p_new;
		this.middle.vOffset      = p_new;
		this.right.vOffset       = p_new;
		this.text_name.vOffset   = p_new + this.size;

		return p_new;
	});


	return true;
}
